package com.matt.repository.impl;

import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.LeadsLinkClienteleNativeRepository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/11/07
 * *******************************
 */
@Service
public class LeadsLinkClienteleNativeRepositoryImpl extends BaseNativeSqlRepository implements LeadsLinkClienteleNativeRepository {


    @Override
    public Integer pushFollowNum(String staffId) {
        String querySql = String.format("SELECT a.leadsCount+b.clienteleCount FROM ");
        querySql+=String.format(" (SELECT count(1) as leadsCount FROM leads as le INNER JOIN leads_history  as lh ON le.id=lh.leadsId " +
                " WHERE le.changeStatus='0' and lh.type='leads' and lh.operationType='follow' and date(lh.nextTime)=date(date_sub(NOW(),interval -1 day)) and le.staffId='%s') as a,",staffId);
        querySql+=String.format(" (SELECT count(1) clienteleCount from clientele as cl INNER JOIN leads_history as lh ON cl.id=lh.clientelesId " +
                " WHERE lh.type='clientele' and lh.operationType='follow' and date(lh.nextTime)=date(date_sub(NOW(),interval -1 day)) and cl.signStatus=0 and cl.staffId='%s') as b",staffId);
        List list = sqlSingleList(querySql);
        if (list.size() > 0)
            return list.get(0) !=null ? Integer.parseInt(list.get(0).toString()) :0 ;
        else
            return 0;
    }
}
